package com.example.wanandroid.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.wanandroid.R;
import com.example.wanandroid.activity.SystemTablayoutActivity;
import com.example.wanandroid.adapter.SystemAdapter;
import com.example.wanandroid.constant.Constant;
import com.example.wanandroid.entity.SystemBean;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.toolbox.Loger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SystemFragment extends Fragment {

    private List<SystemBean> mArrayList=new ArrayList<>();
    private RecyclerView mRecyclerView;
    private SystemAdapter systemAdapter;
    private ArrayList<String> mStringList=new ArrayList<>();
    private String result;
    private String resultPname;
    private String resultPid;
    private ArrayList<String> mPname=new ArrayList<>();
    private ArrayList<String> mPid=new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_system, null);

        initData();
        initView(view);
        return view;
    }

    private void initView(View view) {
        mRecyclerView=view.findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        systemAdapter=new SystemAdapter(getContext(),mArrayList);
        mRecyclerView.setAdapter(systemAdapter);

        systemAdapter.setOnItemClickListener(new SystemAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent=new Intent(getContext(), SystemTablayoutActivity.class);
                intent.putExtra("name",mArrayList.get(position).getName());
                intent.putExtra("mPname",mArrayList.get(position).getmPname());
                intent.putExtra("mPid",mArrayList.get(position).getmPid());
                startActivity(intent);
            }
        });
    }

    private void initData() {
        RxVolley.get(Constant.SYSTEM_DATA, new HttpCallback() {
            @Override
            public void onSuccess(String data) {
                Loger.debug("请求到的数据:" + data);
                if(!TextUtils.isEmpty(data)){
                    getSystemJson(data);
                }
                super.onSuccess(data);
            }
        });
    }

    private void getSystemJson(String data) {
        try {
            JSONObject jsonObject=new JSONObject(data);
            JSONArray jsonArray=jsonObject.getJSONArray("data");
            for (int i=0;i<jsonArray.length();i++){
                SystemBean systemBean=new SystemBean();
                JSONObject jsonData= (JSONObject) jsonArray.get(i);
                JSONArray jsonArray1=jsonData.getJSONArray("children");
                mStringList.clear();
                mPid.clear();
                mPname.clear();
                for (int j=0;j<jsonArray1.length();j++){
                    JSONObject jsonData1= (JSONObject) jsonArray1.get(j);
                    mStringList.add(jsonData1.getString("name"));
                    mPname.add(jsonData1.getString("name"));
                    mPid.add(jsonData1.getString("id"));
                    ArrayList2String(mStringList);
                    ArrayList2StringPname(mPname);
                    ArrayList2StringPid(mPid);
                    systemBean.setContent(result);
                    systemBean.setmPid(resultPid);
                    systemBean.setmPname(resultPname);
                }
                systemBean.setName(jsonData.getString("name"));
                mArrayList.add(systemBean);
                systemAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // ArrayList类型转成String类型
    public String ArrayList2String(ArrayList<String> arrayList) {
        result = "";
        if (arrayList != null && arrayList.size() > 0) {
            for (String item : arrayList) {
                // 把列表中的每条数据用逗号分割开来，然后拼接成字符串
                result += item + "   ";
            }
            // 去掉最后一个逗号
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    // ArrayList类型转成String类型
    public String ArrayList2StringPname(ArrayList<String> arrayList) {
        resultPname = "";
        if (arrayList != null && arrayList.size() > 0) {
            for (String item : arrayList) {
                // 把列表中的每条数据用逗号分割开来，然后拼接成字符串
                resultPname += item + ",";
            }
            // 去掉最后一个逗号
            resultPname = resultPname.substring(0, resultPname.length() - 1);
        }
        return resultPname;
    }

    // ArrayList类型转成String类型
    public String ArrayList2StringPid(ArrayList<String> arrayList) {
        resultPid = "";
        if (arrayList != null && arrayList.size() > 0) {
            for (String item : arrayList) {
                // 把列表中的每条数据用逗号分割开来，然后拼接成字符串
                resultPid += item + ",";
            }
            // 去掉最后一个逗号
            resultPid = resultPid.substring(0, resultPid.length() - 1);
        }
        return resultPid;
    }
}
